package panda.metainfo;

import java.util.Collection;
import java.util.Map;

import panda.record.Schema;
import panda.record.TableToken;
import panda.transaction.Transaction;

/**
 * The manager that handles updates and queries about metadata 
 * @author 
 *
 */
//TODO Please complete this class
public class MetaInfoManager {
	private IndexManager indexmgr;
	private StaticManager statsmgr;
	private TableManager tblmgr;
	private ViewManager viewmgr;
	
	public MetaInfoManager(boolean isNew, Transaction tx) {
		tblmgr = new TableManager(isNew, tx);
		viewmgr = new ViewManager(isNew, tblmgr, tx);
		statsmgr = new StaticManager(tblmgr, tx);
		indexmgr = new IndexManager(isNew, tblmgr, tx);
	}
	
	public void createTable(String name, Schema sch, Transaction tx) {
		tblmgr.createTable(name, sch, tx);
	}
	
	public void deleteTable(String name, Transaction tx) {
		tblmgr.dropTable(name, tx);
	}
	
	/**
	 * Get the token of a table along with its information given the table name
	 * @param tbl the name of the table
	 * @return the token of the table
	 */
	public TableToken getTableToken(String tbl, Transaction tx) {
		return tblmgr.getTableToken(tbl, tx);
	}
	
	public void createView(String name, String def, Transaction tx) {
		viewmgr.createView(name, def, tx);
	}
	
	public String getViewDefinition(String viewname, Transaction tx) {
		return viewmgr.getViewDef(viewname, tx);
	}
	
	/**
	 * Get all the indices on the specified table
	 * @param tbl the name of the specified table  
	 * @return the token of the indices
	 */
	public Map<String, IndexToken> getAllIndices(String tbl, Transaction tx) {
		return indexmgr.getIndexToken(tbl, tx);
	}
	
	/**
	 * Get the statistical information of the specified table
	 * @param tbl
	 * @return
	 */
	public StaticInfo getStaticInfo(String tbl, TableToken tt, Transaction tx) {
		return statsmgr.getStaticInfo(tbl, tt, tx);
	}
	
	/**
	 * Create a new index on the specified attribute of the specified table
	 * @param idxname the name of the index
	 * @param tblname the name of the table on which to build index
	 * @param attrname the attribute on which to build index
	 * @param tx
	 */
	public void createIndex(String idxname, String tblname, String attrname, Transaction tx) {
		indexmgr.createIndex(idxname, tblname, attrname, tx);
	}
	
	/**
	 * Delete the specified index on the specified table
	 * @param idxname the name of the index
	 * @param tblname the name of the table
	 */
	public void deleteIndex(String idxname, Transaction tx) {
		indexmgr.deleteIndex(idxname, tx);
	}
	
	public Collection<String> getAllTableNames(Transaction tx) {
		return tblmgr.getAllTables(tx);
	}
}
